package  
{
  import flash.display.MovieClip;
  import flash.display.Sprite;
  import flash.geom.Point;
  import mge.Timer;
  import mge.ui.Input;
	/**
   * ...
   * @author Ivan
   */
  public class GuyController 
  {
    private const MAX_SPEED:Number = 50;
    private const ACCELERATION:Number = 40;
    private const DECELERATION:Number = 10;
    
    public function GuyController(guy:MovieClip ) 
    {
      this.guy = guy;
      updateTimer.start();
    }
    
    public function update():void
    {
      var delta:Number = updateTimer.getDelta();
      updateTimer.start();
      
      // Updates direction
      var dir:Point;
      
      if (Input.isPressed(Input.KEY_LEFT)/*  && speed.x < 0.1*/)  dir = new Point( -ACCELERATION, 0);
      if (Input.isPressed(Input.KEY_RIGHT)/* && speed.x > -0.1*/) dir = new Point( ACCELERATION, 0);
      
      if (dir)
        speed.x += dir.x * delta;
      if (speed.x > MAX_SPEED) speed.x = MAX_SPEED;
      if (speed.x < -MAX_SPEED) speed.x = -MAX_SPEED;
      
      guy.x += speed.x;
      
      if (guy.x < 0) guy.x = 0;
      if (guy.x > Main.WIDTH) guy.x = Main.WIDTH;
      
      if (!dir)
      {
        speed.x *= (1 - (DECELERATION * delta));
      }
      
      // Updates stunts
      updateStunts();
    }
    
    private function updateStunts():void
    {
      if (Input.isPressed(Input.KEY_DOWN) && guy.currentLabel == "normal")
      {
        guy.gotoAndPlay("stunt_1");
      }
    }
    
    private var updateTimer:Timer = new Timer(1000);
    private var speed:Point = new Point();
    private var guy:MovieClip;
    private var key_pressed:Boolean;
    
  }

}